// 导入node模块
const http = require('http')
const fs = require('fs')
const path = require('path')

const server = http.createServer()

server.on('request', (req, res) => {
  const url = req.url
  let fpath = ''
  if(url === '/') {
    fpath = path.join(__dirname, './clock/index.html')
  } else if(url === '/index.html' || url === '/index.js' || url === '/index.css') {
    fpath = path.join(__dirname, './clock', url)
  } else {
    fpath = path.join(__dirname, url)
  }  
  // res.setHeader('Content-Type', 'text/html; charset=utf-8')
  fs.readFile(fpath, 'utf8', (err, dataStr) => {
    if(err) return res.end('404 Not Found')
    res.end(dataStr)
  })
})

server.listen(80, () => {
  console.log('server running at http://127.0.0.1')
})